package com.matt.service.cookbook.impl;

import com.aliyun.openservices.shade.com.alibaba.fastjson.JSON;
import com.aliyun.openservices.shade.com.alibaba.fastjson.serializer.SerializerFeature;
import com.matt.domain.cookbook.Recipe;
import com.matt.domain.cookbook.RecipeShow;
import com.matt.foundation.utils.DateUtil;
import com.matt.repository.cookbook.RecipeNativeRepository;
import com.matt.repository.cookbook.RecipeShowRepository;
import com.matt.service.cookbook.RecipeService;
import com.matt.service.cookbook.RecipeShowService;
import org.jsoup.helper.DataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RecipeShowServiceImpl implements RecipeShowService {

    @Autowired
    RecipeShowRepository recipeShowRepository;

    @Autowired
    RecipeNativeRepository recipeNativeRepository;

    @Autowired
    RecipeService recipeService;

    @Override
    public void deleteRecipeShowByTimeAndAreaId(String recipeDateStr, String areaId) {
        Date recipeDate = DateUtil.parseToDate(recipeDateStr);
        recipeShowRepository.deleteByRecipeDateAndAreaId(recipeDate, areaId);
    }

    @Override
    public List<RecipeShow> selectRecipeListByCondition(Map<String, Object> filter) {
        List<RecipeShow> list = recipeNativeRepository.selectRecipeShowListByCondition(filter);
        return list;
    }

    @Override
    public RecipeShow selectRecipeShowByTimeAndSchoolId(String recipeDateStr, String areaId) {
        Date recipeDate = DateUtil.parseToDate(recipeDateStr);
        return recipeShowRepository.findFirstByRecipeDateAndAreaId(recipeDate, areaId);
    }

    @Override
    public RecipeShow createRecipeShow(String areaId, Date date, String json) {
        RecipeShow recipeShow =  new RecipeShow();
        recipeShow.setAreaId(areaId);
        recipeShow.setRecipeDate(date);
        recipeShow.setContent(json);
//          recipeShow.setCreater(1);  // for ybb
        recipeShow.setPublishFlag(1);   //总是默认发布状态

        recipeShowRepository.save(recipeShow);

        return  recipeShow;
    }

    @Override
    public String createRecipeShowJson(String recipeDate, String areaId) {

        List<Map<String, Object>> recipeTypeIdMapList = recipeService.selectRecipeTypeIdByTimeAndAreaId(recipeDate, areaId);

        return createRecipeShowJson(recipeTypeIdMapList, areaId, recipeDate);
    }

    @Override
    public String createRecipeShowJson(List<Map<String, Object>> recipeTypeIdMapList, String areaId, String recipeDate){

        String recipeShowJson = "";

        List jsonArr_recipeShow = new ArrayList();

        for(Map recipeTypeMap : recipeTypeIdMapList){
            List<Map> menus = new ArrayList();

            Map<String, Object> filter = new HashMap<String, Object>();
            filter.put("areaId", areaId);
            filter.put("recipeDate", recipeDate);
            filter.put("recipeTypeId",recipeTypeMap.get("recipeTypeId"));

            List<Map<String, Object>> list = recipeService.selectRecipeFoodByFilter(filter);/**餐次配置的食物 比如 早餐有什么**/
            Map jsonObj = new HashMap();
            jsonObj.put("meal",     recipeTypeMap.get("recipeTypeId"));
            jsonObj.put("mealname", recipeTypeMap.get("recipeTypeName"));
            for(Map m : list){
                Map menus_food  = new HashMap();
                menus_food.put("menuId", 		m.get("foodId"));
                menus_food.put("menuImageUrl", 	m.get("photo"));
                menus_food.put("menuName",     	m.get("foodName"));
                menus_food.put("ingredients",  	new ArrayList<>());
                menus.add(menus_food);

                jsonObj.put("menus", menus);
            }
            jsonArr_recipeShow.add(jsonObj);

        }

        if(recipeTypeIdMapList.size() !=0){
            recipeShowJson = JSON.toJSONString(jsonArr_recipeShow, SerializerFeature.DisableCircularReferenceDetect);
        }
        return recipeShowJson;
    }


}
